﻿namespace OsPortal
{
    using System;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.IO;
    using System.Runtime.InteropServices;
    using System.Web;
    using System.Web.UI.WebControls;

    public class oFileHelper
    {
        private static string strClassName = "oFileHelper";

        public static bool ClearFolder(string strPath)
        {
            try
            {
                DirectoryInfo info = new DirectoryInfo(strPath);
                foreach (FileInfo info2 in info.GetFiles())
                {
                    info2.Delete();
                }
                foreach (DirectoryInfo info3 in info.GetDirectories())
                {
                    ClearFolder(info3.FullName);
                    info3.Delete();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool CreateFolder(string strPath)
        {
            try
            {
                if (!Directory.Exists(HttpContext.Current.Server.MapPath(strPath)))
                {
                    Directory.CreateDirectory(HttpContext.Current.Server.MapPath(strPath));
                }
                else
                {
                    return false;
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool DeleteFile(string strpath)
        {
            try
            {
                string path = HttpContext.Current.Server.MapPath(strpath);
                if (File.Exists(path))
                {
                    File.Delete(path);
                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
            return false;
        }

        public static bool DeleteFolder(string strPath)
        {
            try
            {
                Directory.Delete(HttpContext.Current.Server.MapPath(strPath), true);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static DataTable fncGetLstFileFromFolder(string lstType, string strPath)
        {
            FileInfo[] files = new DirectoryInfo(HttpContext.Current.Server.MapPath(strPath)).GetFiles(lstType);
            DataTable table = new DataTable();
            table.Columns.Add("FileName", typeof(string));
            table.Columns.Add("Size", typeof(long));
            table.Columns.Add("Path", typeof(string));
            table.Columns.Add("CreateDate", typeof(DateTime));
            foreach (FileInfo info2 in files)
            {
                table.Rows.Add(new object[] { info2.Name, info2.Length, info2.Directory.ToString().Replace(HttpContext.Current.Server.MapPath("~"), ""), info2.CreationTime });
            }
            return table;
        }

        public static DataTable fncGetLstFileTextInFolder(string strPath)
        {
            return fncGetLstFileFromFolder("*.txt", strPath);
        }

        public static DataTable fncGetLstImageInFolder(string strPath)
        {
            return fncGetLstFileFromFolder("*.jpg,*.gif,*.png,*.bmp", strPath);
        }

        public static string ReadFile(string strPath)
        {
            string str = "";
            try
            {
                string path = Path.Combine(HttpRuntime.AppDomainAppPath, strPath);
                if (File.Exists(path))
                {
                    using (StreamReader reader = new StreamReader(path))
                    {
                        str = reader.ReadToEnd();
                        reader.Close();
                    }
                    return str;
                }
                return "";
            }
            catch (Exception exception)
            {
                WriteLogErr(strClassName, "ReadFile", exception.ToString());
                return "";
            }
            return str;
        }

        public static bool UploadAnhResizeImage(FileUpload fileUpload, string strPath, int width, out string strImgName, string strText, bool blInsertText)
        {
            if (fileUpload.HasFile)
            {
                int num3;
                int num4;
                string fileName = fileUpload.FileName;
                string str2 = fileName.Substring(fileName.LastIndexOf(".") + 1);
                if (!Directory.Exists(HttpContext.Current.Server.MapPath(strPath)))
                {
                    Directory.CreateDirectory(HttpContext.Current.Server.MapPath(strPath));
                }
                string str3 = HttpContext.Current.Server.MapPath(strPath + "/");
                Bitmap original = new Bitmap(fileUpload.FileContent);
                int num = original.Width;
                int height = original.Height;
                if (num > width)
                {
                    num3 = width;
                    num4 = (num3 * height) / num;
                }
                else
                {
                    num3 = num;
                    num4 = height;
                }
                Bitmap image = new Bitmap(original, num3, num4);
                Graphics graphics = Graphics.FromImage(image);
                graphics.SmoothingMode = SmoothingMode.AntiAlias;
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics.DrawImage(original, 0, 0, num3, num4);
                if (blInsertText)
                {
                    int alpha = 200;
                    Font font = new Font("Tahoma", 20f);
                    SolidBrush brush = new SolidBrush(Color.FromArgb(alpha, Color.Red));
                    PointF point = new PointF(10f, 10f);
                    graphics.DrawString(strText, font, brush, point);
                }
                DateTime now = DateTime.Now;
                string str4 = Convert.ToString(string.Concat(new object[] { now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second, now.Millisecond }));
                str4 = "smail_" + str4 + "." + str2;
                image.Save(str3 + str4);
                original.Dispose();
                image.Dispose();
                graphics.Dispose();
                strImgName = str4;
                return true;
            }
            strImgName = "";
            return false;
        }

        public static void WriteFile(string strContent, string strPath)
        {
            try
            {
                string path = HttpContext.Current.Server.MapPath(strPath);
                if (!File.Exists(path))
                {
                    FileStream stream = null;
                    using (stream = File.Create(path))
                    {
                    }
                }
                File.WriteAllText(path, strContent);
                using (TextWriter writer = new StreamWriter(path, false))
                {
                    writer.WriteLine(strContent);
                    writer.Close();
                }
            }
            catch
            {
            }
        }

        public static void WriteFileAppend(string strContent, string strPath)
        {
            try
            {
                string path = HttpContext.Current.Server.MapPath(strPath);
                if (!File.Exists(path))
                {
                    FileStream stream = null;
                    using (stream = File.Create(path))
                    {
                    }
                }
                using (TextWriter writer = File.AppendText(path))
                {
                    writer.WriteLine(strContent);
                    writer.Close();
                }
            }
            catch
            {
            }
        }

        public static void WriteLogErr(string strFileName, string strFunctionName, string strErr)
        {
            string strPath = "logs.txt";
            WriteFileAppend(DateTime.Now.ToString() + " - " + strFileName + @"\" + strFunctionName + @"\ =>>" + strErr, strPath);
        }
    }
}

